const express = require('express')
const app = express()

// 中间件
const middleware = (req, res, next) => {
  console.log('middleware 处理中……')
  next()
}

// 中间件二
const middleware2 = (req, res, next) => {
  console.log('middleware2 处理中……')
  next()
}

/**
 * 注册局部中间件
 *
 * http://127.0.0.1/api/get
 */
app.get('/api/get', middleware, (req, res) => {
  res.send('Hello world!')
})

/**
 * 注册多个局部中间件 方式一
 *
 * http://127.0.0.1/api/post
 */
app.post('/api/post', middleware, middleware2, (req, res) => {
  res.send('Hello world!')
})

/**
 * 注册多个局部中间件 方式二
 *
 * http://127.0.0.1/api/put
 */
const middlewares = [middleware, middleware2]
app.put('/api/put', middlewares, (req, res) => {
  res.send('Hello world!')
})

app.listen(80, () => {
  console.log('Server running at http://127.0.0.1')
})
